﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataCentral.Dataset;
using CommonData.CommonItems;
using CommonData;

namespace DataCentral.UserControls
{
    public partial class EditSaleRouteDetailControl : DataCentral.CommonCenterControl
    {
        private decimal _CurrentRouteID = 0;
        public EditSaleRouteDetailControl()
        {
            InitializeComponent();
        }

        private void AddSaleTeamToSaleRouteDetail(decimal saleTeamID,string saleRoute)
        {
            CommonDataAccess cmDataAccess = new CommonDataAccess();
            List<ParameterObject> lstParams = new List<ParameterObject>();
            lstParams.Add(new ParameterObject("@SlsTeamID",saleTeamID));
            DataTable tblSalePerson = cmDataAccess.GetDataTableBySproc("DATA_GetSalePersonBySaleTeam",lstParams);
            for(int i=0;i<tblSalePerson.Rows.Count;i++)
            {
                if (dsDataCentral.SaleRouteDetail.Select("SalePersonID = " + tblSalePerson.Rows[i]["SalePersonID"].ToString()).Length == 0)
                {
                    DataCentralDataset.SaleRouteDetailRow drNewRouteDetail = dsDataCentral.SaleRouteDetail.NewSaleRouteDetailRow();
                    drNewRouteDetail.Name = string.Format("{0}-{1}-{2}", saleRoute, saleTeamID, tblSalePerson.Rows[i]["SalePersonID"]);
                    drNewRouteDetail.SalesRouteID = _CurrentRouteID;
                    drNewRouteDetail.SalePersonID = Convert.ToDecimal(tblSalePerson.Rows[i]["SalePersonID"]);
                    drNewRouteDetail.SlsTeamID = saleTeamID;
                    dsDataCentral.SaleRouteDetail.AddSaleRouteDetailRow(drNewRouteDetail);
                }
            }
            daSaleRouteDetail.Update(dsDataCentral.SaleRouteDetail);
        }

        private void EditSaleRouteDetailControl_Load(object sender, EventArgs e)
        {
            ValueListClass clValueList = new ValueListClass();
            daSaleRoute.Fill(dsDataCentral.SalesRoute);

            iluSalePerson.DataSource = clValueList.GetSaleSysPersonList();
            iluSaleTeam.Properties.DataSource = clValueList.GetSaleTeamList();
            iluSaleTeam1.DataSource = iluSaleTeam.Properties.DataSource;
        }

        private void viewRoute_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (viewRoute.GetDataRow(viewRoute.FocusedRowHandle) is DataCentralDataset.SalesRouteRow)
            {
                _CurrentRouteID = ((DataCentralDataset.SalesRouteRow)viewRoute.GetDataRow(viewRoute.FocusedRowHandle)).SalesRouteID;
                daSaleRouteDetail.Fill(dsDataCentral.SaleRouteDetail, _CurrentRouteID);
            }
        }

        private void iluSaleTeam_EditValueChanged(object sender, EventArgs e)
        {
            btnAddSaleTeam.Enabled = iluSaleTeam.EditValue != null;
        }

        private void btnAddSaleTeam_Click(object sender, EventArgs e)
        {
            if (iluSaleTeam.EditValue!=null)
                AddSaleTeamToSaleRouteDetail(Convert.ToDecimal(iluSaleTeam.EditValue),viewRoute.GetFocusedRowCellDisplayText(colName));

        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
           // btnDelete.PerformClick();
            daSaleRouteDetail.Update(dsDataCentral.SaleRouteDetail);
        }

        private void viewRouteDetail_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            daSaleRouteDetail.Update(dsDataCentral.SaleRouteDetail);
        }

        private void viewRouteDetail_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                btnDelete_Click(null,null);
            }
            else if (e.KeyCode == Keys.Insert)
            {
                btnAdd.PerformClick();
            }
        }

        private void viewRouteDetail_InvalidRowException(object sender, DevExpress.XtraGrid.Views.Base.InvalidRowExceptionEventArgs e)
        {
            MessageClass.ShowErrorMessage(ConstantClass._Error_InvalidData);
            e.ExceptionMode = DevExpress.XtraEditors.Controls.ExceptionMode.NoAction;
        }

        private bool IsRouteDetailExisted(DataCentralDataset.SaleRouteDetailRow slsRouteDetailRow)
        {
            bool isExist = false;
            //foreach (DataCentralDataset.RoleRow drSelect in dsBET.Role)
            //{
            //    if (drSelect.RoleID != drRole.RoleID && drSelect.RoleName.Equals(drRole.RoleName))
            //    {
            //        isExist = true;
            //        break;
            //    }
            //}
            return isExist;
        }

        private void viewRouteDetail_ValidateRow(object sender, DevExpress.XtraGrid.Views.Base.ValidateRowEventArgs e)
        {
            DataCentralDataset.SaleRouteDetailRow slsRouteDetailRow;

            if (viewRouteDetail.GetFocusedDataRow() is DataCentralDataset.SaleRouteDetailRow)
            {
                viewRouteDetail.ClearColumnErrors();
                slsRouteDetailRow = (DataCentralDataset.SaleRouteDetailRow)viewRouteDetail.GetFocusedDataRow();

                if (slsRouteDetailRow.IsNull("Name") || string.IsNullOrEmpty(slsRouteDetailRow.Name))
                {
                    viewRouteDetail.SetColumnError(colName1, "Tên không được để trống");
                    viewRouteDetail.FocusedColumn = colName1;
                    e.Valid = false;
                }
                else if (!string.IsNullOrEmpty(slsRouteDetailRow.Name) && IsRouteDetailExisted(slsRouteDetailRow))
                {
                    viewRouteDetail.SetColumnError(colName1, "Tên không được trùng nhau");
                    viewRouteDetail.FocusedColumn = colName1;
                    e.Valid = false;
                }
            }
        }
    }
}
